<?php

namespace App\Services\Sms;

use App\Models\Common\MobileLog;
use App\Services\Com\LogService;
use Illuminate\Support\Facades\Cache;
use Qcloud\Sms\SmsSingleSender;

class PlainService
{
    public static function send($mobile, $templateId, $param = [])
    {
        $mobile = trim($mobile);

        $data = [];
        $data['mobile'] = $mobile;
        $data['template_id'] = $templateId;
        $data['param'] = json_encode($param);

        try {
            $sender = new SmsSingleSender(config('sms.SMS_AP_PID'), config('sms.SMS_APP_KEY'));
            $result = $sender->sendWithParam("86", $mobile, $templateId, $param, "", "", "");
            $rsp = json_decode($result, true);
        }
        catch (\Exception $e) {
            LogService::error('Sms Error1: ' . $mobile.$e->getMessage());

            $data['result'] = '发送失败';
            $data['msg'] = 'Sms Error1: ' . $mobile.$e->getMessage();
            MobileLog::create($data);

            return false;
        }

        // 网关消息
        if (isset($rsp['ActionStatus']) && 'FAIL' == $rsp['ActionStatus']) {
            LogService::error('Sms Error2: ' . $mobile.$rsp['ErrorCode'] . ' ' . $rsp['ErrorInfo']);

            $data['result'] = '发送失败';
            $data['msg'] = 'Sms Error2: ' . $mobile.$rsp['ErrorCode'] . ' ' . $rsp['ErrorInfo'];
            MobileLog::create($data);

            return false;
        }

        // 网关消息
        if (isset($rsp['result']) && $rsp['result']) {
            LogService::error('Sms Error3: ' . $mobile.json_encode($rsp));

            $data['result'] = '发送失败';
            $data['msg'] = 'Sms Error3: ' . $mobile.json_encode($rsp);
            MobileLog::create($data);

            return false;
        }

        $data['result'] = '发送成功';
        MobileLog::create($data);

        return true;
    }
    public static function spreeByOld($mobile, $param = [])
    {
        try {
            $sender = new SmsSingleSender(config('sms.SMS_AP_PID'), config('sms.SMS_APP_KEY'));
            $result = $sender->sendWithParam("86", $mobile, config('sms.tpl_id.SMS_TEMPLATE_SPREE_OLD'), $param, "", "", "");
            $rsp = json_decode($result, true);
        }
        catch (\Exception $e) {
            LogService::error('Sms Error1: ' . $e->getMessage());

            return false;
        }

        // 网关消息
        if (isset($rsp['ActionStatus']) && 'FAIL' == $rsp['ActionStatus']) {
            LogService::error('Sms Error2: ' . $rsp['ErrorCode'] . ' ' . $rsp['ErrorInfo']);

            return false;
        }

        // 网关消息
        if (isset($rsp['result']) && $rsp['result']) {
            LogService::error('Sms Error3: ' . json_encode($rsp));

            return false;
        }

        return true;
    }

    public static function spreeByRegister($mobile, $param = [])
    {
        try {
            $sender = new SmsSingleSender(config('sms.SMS_AP_PID'), config('sms.SMS_APP_KEY'));
            $result = $sender->sendWithParam("86", $mobile, config('sms.tpl_id.SMS_TEMPLATE_SPREE_REGISTER'), $param, "", "", "");
            $rsp = json_decode($result, true);
        }
        catch (\Exception $e) {
            LogService::error('Sms Error1: ' . $e->getMessage());

            return false;
        }

        // 网关消息
        if (isset($rsp['ActionStatus']) && 'FAIL' == $rsp['ActionStatus']) {
            LogService::error('Sms Error2: ' . $rsp['ErrorCode'] . ' ' . $rsp['ErrorInfo']);

            return false;
        }

        // 网关消息
        if (isset($rsp['result']) && $rsp['result']) {
            LogService::error('Sms Error3: ' . json_encode($rsp));

            return false;
        }

        return true;
    }

    /**
     * 验证短信验证码
     *
     * @param $mobile
     * @param $code
     * @return bool
     */
    public static function verify($mobile, $code)
    {
        // 验证手机号
       /* if (11 !== strlen($mobile) || !preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile)) {
            return false;
        }*/

        // 验证验证码
        if (4 !== strlen($code) || !Cache::has("sms_captcha_{$mobile}") || md5($code) !== Cache::get("sms_captcha_{$mobile}")) {
            return false;
        }

        // 清除缓存
        Cache::forget("sms_captcha_{$mobile}");

        return true;
    }

    /**
     * 是否拥有有效短信验证码
     *
     * @param $mobile
     * @return bool
     */
    public static function has($mobile)
    {
        return Cache::has("sms_captcha_{$mobile}");
    }
}
